package com.fjasmin.activitytest.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

/**
 * @Author qianciling
 * @Date 2021/6/30
 * @Describe sp工具类
 */
public final class SpUtils {
    private static final String SP_NAME = "kvs.app";
    private SharedPreferences sp;

    private static volatile SpUtils Instance;

    public static synchronized void init(@NonNull Context context) {
        if (SpUtils.Instance == null) {//仅允许初始化一次
            SpUtils.Instance = new SpUtils(context);
        }
    }

    private SpUtils(@NonNull Context context) {
        sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    private static final String SP_TOKEN = "Token";
    private static final String SP_TIME = "Time";

    public static String getToken() {
        return Instance.sp.getString(SP_TOKEN, "");
    }

    public static long getTime() {
        return Instance.sp.getLong(SP_TIME, 0L);
    }

    public static void setToken(String token){
        Instance.sp.edit().putString(SP_TOKEN, token)
                .putLong(SP_TIME, System.currentTimeMillis())
                .apply();
    }
}
